package org.nowcoder.leetcode.stack;

import java.util.Stack;

/**
 * Title  : 155. Min Stack
 * Source : https://leetcode.cn/problems/min-stack/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-07-12
 */

//chatGPT 解答源码
public class LeetCode_155 {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;

    public LeetCode_155() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int x) {
        stack.push(x);
        if (minStack.isEmpty() || x <= minStack.peek()) {
            minStack.push(x);
        }
    }

    public void pop() {
        if (stack.pop().equals(minStack.peek())) {
            minStack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}
